/*
 * jsio.h
 *
 *  Created on: 2009-6-29
 *      Author: root
 */

#ifndef JSIOMODEL_H_
#define JSIOMODEL_H_

#define JSMAXPOOLSIZE	1024
#define JSMAXIOEVENTCOUNT 100

#include <arpa/inet.h>
#include <sys/time.h>
#include "signal.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>

#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>

#include <pthread.h>
#include <iostream>
using namespace std;

/*
enum EFdEvent
{JSREADEVENT,JSWRITEEVENT,JSSENDEVENT,JSRECVEVENT,JSCONNECTEVENT};
*/

//FD EVENT
#define JSREADEVENT			0x00000001
#define JSWRITEEVENT		0x00000002
#define JSEXCEPTIONEVENT	0x00000003


#define  JSRD				0x00000001
#define  JSWR               0x00000002
#define  JSEX               0x00000004
#define  JSRDWR             0x00000007


#define JSREADMASK			0x00000001
#define JSWRITEMASK			0x00000002
#define JSEXCEPTIONMASK		0x00000004

#define JSNETEVENTMASK		0x00010000
#define JSNETEVENT			0x00010007
#define JSNETRE				0x00010001
#define JSNETWR				0x00010002
#define JSNETEX				0x00010004

#define	JSCONNECTEVENTMASK	0x00020000
#define JSCONNECTEVENT		0x00020001

#define JSISSETMASK(mask,event)	(((mask)&(event))==(mask))


typedef  int(*EventCallBack)(int fd,int event,void *param) ;

struct JSFdEvent
{
	int fd;
	int event;
	EventCallBack eventCallBack;
};

class CJSIOModel
{
public:
	CJSIOModel();
	~CJSIOModel();

	int AddNewFd(int fd,int event,EventCallBack eventCallBack);
	int ClearFd(int fd);
	static void* Work(void* arg);
private:

	JSFdEvent pool[JSMAXPOOLSIZE];
	int m_maxPoolIndex;
	
	fd_set m_rdSet;
	fd_set m_wrSet;
	fd_set m_exSet;
	int m_maxFd;

	pthread_mutex_t m_mutex;
	pthread_t m_workThread;
};

#endif /* JSIOMODEL_H_ */
